﻿using Microsoft.VisualBasic;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace libVNEditor
{
    public class CEvent : Form
    {
        //事件的类型
        public string type
        {
            set
            {
                comboBox_EventType.Text = value;
            }
            get
            {
                return comboBox_EventType.Text;
            }
        }
        //事件的名称
        public string name
        {
            set
            {
                textBox_EventName.Text = value;
            }
            get
            {
                return textBox_EventName.Text;
            }
        }
        //这个事件发送后执行的动作序列
        public List<CAction> actions = new List<CAction>();

        public override string ToString()
        {
            string str;
            str = "<event type=\"" + type + "\" name=\"" + name + "\">\n";
            foreach(CAction action in actions)
            {
                str += action.ToString();
            }
            str += "</event>\n";
            return str;
        }

        private void InitializeComponent()
        {
            this.tableLayoutPanel1 = new System.Windows.Forms.TableLayoutPanel();
            this.label1 = new System.Windows.Forms.Label();
            this.label2 = new System.Windows.Forms.Label();
            this.textBox_EventName = new System.Windows.Forms.TextBox();
            this.comboBox_EventType = new System.Windows.Forms.ComboBox();
            this.tableLayoutPanel2 = new System.Windows.Forms.TableLayoutPanel();
            this.label3 = new System.Windows.Forms.Label();
            this.label4 = new System.Windows.Forms.Label();
            this.tableLayoutPanel3 = new System.Windows.Forms.TableLayoutPanel();
            this.btn_MoveUp = new System.Windows.Forms.Button();
            this.btn_DeleteAction = new System.Windows.Forms.Button();
            this.btn_MoveDown = new System.Windows.Forms.Button();
            this.btn_CreateAction = new System.Windows.Forms.Button();
            this.listBox_actions = new System.Windows.Forms.ListBox();
            this.tableLayoutPanel1.SuspendLayout();
            this.tableLayoutPanel2.SuspendLayout();
            this.tableLayoutPanel3.SuspendLayout();
            this.SuspendLayout();
            // 
            // tableLayoutPanel1
            // 
            this.tableLayoutPanel1.ColumnCount = 4;
            this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 80F));
            this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 120F));
            this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 80F));
            this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 100F));
            this.tableLayoutPanel1.Controls.Add(this.label1, 0, 0);
            this.tableLayoutPanel1.Controls.Add(this.label2, 2, 0);
            this.tableLayoutPanel1.Controls.Add(this.textBox_EventName, 3, 0);
            this.tableLayoutPanel1.Controls.Add(this.comboBox_EventType, 1, 0);
            this.tableLayoutPanel1.Dock = System.Windows.Forms.DockStyle.Top;
            this.tableLayoutPanel1.Location = new System.Drawing.Point(0, 0);
            this.tableLayoutPanel1.Name = "tableLayoutPanel1";
            this.tableLayoutPanel1.RowCount = 1;
            this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 100F));
            this.tableLayoutPanel1.Size = new System.Drawing.Size(538, 31);
            this.tableLayoutPanel1.TabIndex = 0;
            // 
            // label1
            // 
            this.label1.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Left | System.Windows.Forms.AnchorStyles.Right)));
            this.label1.AutoSize = true;
            this.label1.Location = new System.Drawing.Point(3, 9);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(74, 12);
            this.label1.TabIndex = 0;
            this.label1.Text = "事件类型";
            // 
            // label2
            // 
            this.label2.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Left | System.Windows.Forms.AnchorStyles.Right)));
            this.label2.AutoSize = true;
            this.label2.Location = new System.Drawing.Point(203, 9);
            this.label2.Name = "label2";
            this.label2.Size = new System.Drawing.Size(74, 12);
            this.label2.TabIndex = 1;
            this.label2.Text = "事件名称";
            // 
            // textBox_EventName
            // 
            this.textBox_EventName.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Left | System.Windows.Forms.AnchorStyles.Right)));
            this.textBox_EventName.Location = new System.Drawing.Point(283, 5);
            this.textBox_EventName.Name = "textBox_EventName";
            this.textBox_EventName.Size = new System.Drawing.Size(252, 21);
            this.textBox_EventName.TabIndex = 2;
            // 
            // comboBox_EventType
            // 
            this.comboBox_EventType.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Left | System.Windows.Forms.AnchorStyles.Right)));
            this.comboBox_EventType.FormattingEnabled = true;
            this.comboBox_EventType.Items.AddRange(new object[] {
            "选项",
            "句子结束"});
            this.comboBox_EventType.Location = new System.Drawing.Point(83, 5);
            this.comboBox_EventType.Name = "comboBox_EventType";
            this.comboBox_EventType.Size = new System.Drawing.Size(114, 20);
            this.comboBox_EventType.TabIndex = 3;
            // 
            // tableLayoutPanel2
            // 
            this.tableLayoutPanel2.ColumnCount = 2;
            this.tableLayoutPanel2.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 80F));
            this.tableLayoutPanel2.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 100F));
            this.tableLayoutPanel2.Controls.Add(this.label3, 0, 0);
            this.tableLayoutPanel2.Controls.Add(this.label4, 0, 1);
            this.tableLayoutPanel2.Controls.Add(this.tableLayoutPanel3, 1, 0);
            this.tableLayoutPanel2.Controls.Add(this.listBox_actions, 1, 1);
            this.tableLayoutPanel2.Dock = System.Windows.Forms.DockStyle.Fill;
            this.tableLayoutPanel2.Location = new System.Drawing.Point(0, 31);
            this.tableLayoutPanel2.Name = "tableLayoutPanel2";
            this.tableLayoutPanel2.RowCount = 2;
            this.tableLayoutPanel2.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Absolute, 35F));
            this.tableLayoutPanel2.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 100F));
            this.tableLayoutPanel2.Size = new System.Drawing.Size(538, 230);
            this.tableLayoutPanel2.TabIndex = 1;
            // 
            // label3
            // 
            this.label3.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Left | System.Windows.Forms.AnchorStyles.Right)));
            this.label3.AutoSize = true;
            this.label3.Location = new System.Drawing.Point(3, 11);
            this.label3.Name = "label3";
            this.label3.Size = new System.Drawing.Size(74, 12);
            this.label3.TabIndex = 1;
            this.label3.Text = "动作控制";
            // 
            // label4
            // 
            this.label4.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Left | System.Windows.Forms.AnchorStyles.Right)));
            this.label4.AutoSize = true;
            this.label4.Location = new System.Drawing.Point(3, 126);
            this.label4.Name = "label4";
            this.label4.Size = new System.Drawing.Size(74, 12);
            this.label4.TabIndex = 2;
            this.label4.Text = "动作列表";
            // 
            // tableLayoutPanel3
            // 
            this.tableLayoutPanel3.ColumnCount = 5;
            this.tableLayoutPanel3.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 80F));
            this.tableLayoutPanel3.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 80F));
            this.tableLayoutPanel3.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 80F));
            this.tableLayoutPanel3.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 80F));
            this.tableLayoutPanel3.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 100F));
            this.tableLayoutPanel3.Controls.Add(this.btn_MoveUp, 0, 0);
            this.tableLayoutPanel3.Controls.Add(this.btn_DeleteAction, 0, 0);
            this.tableLayoutPanel3.Controls.Add(this.btn_MoveDown, 1, 0);
            this.tableLayoutPanel3.Controls.Add(this.btn_CreateAction, 0, 0);
            this.tableLayoutPanel3.Dock = System.Windows.Forms.DockStyle.Fill;
            this.tableLayoutPanel3.Location = new System.Drawing.Point(83, 3);
            this.tableLayoutPanel3.Name = "tableLayoutPanel3";
            this.tableLayoutPanel3.RowCount = 1;
            this.tableLayoutPanel3.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 100F));
            this.tableLayoutPanel3.Size = new System.Drawing.Size(452, 29);
            this.tableLayoutPanel3.TabIndex = 3;
            // 
            // btn_MoveUp
            // 
            this.btn_MoveUp.Dock = System.Windows.Forms.DockStyle.Fill;
            this.btn_MoveUp.Enabled = false;
            this.btn_MoveUp.Location = new System.Drawing.Point(163, 3);
            this.btn_MoveUp.Name = "btn_MoveUp";
            this.btn_MoveUp.Size = new System.Drawing.Size(74, 23);
            this.btn_MoveUp.TabIndex = 3;
            this.btn_MoveUp.Text = "上移动作";
            this.btn_MoveUp.UseVisualStyleBackColor = true;
            this.btn_MoveUp.Click += new System.EventHandler(this.MoveUpActionClick);
            // 
            // btn_DeleteAction
            // 
            this.btn_DeleteAction.Dock = System.Windows.Forms.DockStyle.Fill;
            this.btn_DeleteAction.Enabled = false;
            this.btn_DeleteAction.Location = new System.Drawing.Point(83, 3);
            this.btn_DeleteAction.Name = "btn_DeleteAction";
            this.btn_DeleteAction.Size = new System.Drawing.Size(74, 23);
            this.btn_DeleteAction.TabIndex = 2;
            this.btn_DeleteAction.Text = "删除动作";
            this.btn_DeleteAction.UseVisualStyleBackColor = true;
            this.btn_DeleteAction.Click += new System.EventHandler(this.DeleteActionClick);
            // 
            // btn_MoveDown
            // 
            this.btn_MoveDown.Dock = System.Windows.Forms.DockStyle.Fill;
            this.btn_MoveDown.Enabled = false;
            this.btn_MoveDown.Location = new System.Drawing.Point(243, 3);
            this.btn_MoveDown.Name = "btn_MoveDown";
            this.btn_MoveDown.Size = new System.Drawing.Size(74, 23);
            this.btn_MoveDown.TabIndex = 1;
            this.btn_MoveDown.Text = "下移动作";
            this.btn_MoveDown.UseVisualStyleBackColor = true;
            this.btn_MoveDown.Click += new System.EventHandler(this.MoveDownActionClick);
            // 
            // btn_CreateAction
            // 
            this.btn_CreateAction.Dock = System.Windows.Forms.DockStyle.Fill;
            this.btn_CreateAction.Location = new System.Drawing.Point(3, 3);
            this.btn_CreateAction.Name = "btn_CreateAction";
            this.btn_CreateAction.Size = new System.Drawing.Size(74, 23);
            this.btn_CreateAction.TabIndex = 0;
            this.btn_CreateAction.Text = "创建动作";
            this.btn_CreateAction.UseVisualStyleBackColor = true;
            this.btn_CreateAction.Click += new System.EventHandler(this.CreateActionClick);
            // 
            // listBox_actions
            // 
            this.listBox_actions.Dock = System.Windows.Forms.DockStyle.Fill;
            this.listBox_actions.FormattingEnabled = true;
            this.listBox_actions.ItemHeight = 12;
            this.listBox_actions.Location = new System.Drawing.Point(83, 38);
            this.listBox_actions.Name = "listBox_actions";
            this.listBox_actions.Size = new System.Drawing.Size(452, 189);
            this.listBox_actions.TabIndex = 4;
            this.listBox_actions.SelectedIndexChanged += new System.EventHandler(this.OnSelectedActionChanged);
            this.listBox_actions.DoubleClick += new System.EventHandler(this.OnEdit);
            // 
            // cEvent
            // 
            this.ClientSize = new System.Drawing.Size(538, 261);
            this.Controls.Add(this.tableLayoutPanel2);
            this.Controls.Add(this.tableLayoutPanel1);
            this.Name = "cEvent";
            this.Text = "事件";
            this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.cEvent_FormClosing);
            this.Load += new System.EventHandler(this.cEvent_Load);
            this.tableLayoutPanel1.ResumeLayout(false);
            this.tableLayoutPanel1.PerformLayout();
            this.tableLayoutPanel2.ResumeLayout(false);
            this.tableLayoutPanel2.PerformLayout();
            this.tableLayoutPanel3.ResumeLayout(false);
            this.ResumeLayout(false);

        }

        private TableLayoutPanel tableLayoutPanel1;
        private Label label1;
        private Label label2;
        private TextBox textBox_EventName;
        private ComboBox comboBox_EventType;
        private TableLayoutPanel tableLayoutPanel2;
        private Label label3;
        private Label label4;
        private TableLayoutPanel tableLayoutPanel3;
        private Button btn_MoveUp;
        private Button btn_DeleteAction;
        private Button btn_MoveDown;
        private Button btn_CreateAction;
        private ListBox listBox_actions;

        public CEvent()
        {
            InitializeComponent();
        }
        public void RefreshForm()
        {
            listBox_actions.Items.Clear();
            foreach(CAction action in actions)
            {
                listBox_actions.Items.Add(action.command);
            }
        }

        private void OnSelectedActionChanged(object sender, EventArgs e)
        {
            int idx = listBox_actions.SelectedIndex;
            if (idx == -1)
            {
                btn_MoveUp.Enabled = false;
                btn_MoveDown.Enabled = false;
                btn_DeleteAction.Enabled = false;
            }
            else if (idx == 0)
            {
                btn_DeleteAction.Enabled = true;
                if (idx == listBox_actions.Items.Count - 1)
                {
                    btn_MoveUp.Enabled = false;
                    btn_MoveDown.Enabled = false;
                }
                else if (idx < listBox_actions.Items.Count - 1)
                {
                    btn_MoveDown.Enabled = true;
                    btn_MoveUp.Enabled = false;
                }
            }
            else if (idx == listBox_actions.Items.Count - 1)
            {
                btn_DeleteAction.Enabled = true;
                if (idx == 0)
                {
                    btn_MoveUp.Enabled = false;
                    btn_MoveDown.Enabled = false;
                }
                else if (idx > 0)
                {
                    btn_MoveDown.Enabled = false;
                    btn_MoveUp.Enabled = true;
                }
            }
            else
            {
                btn_DeleteAction.Enabled = true;
                btn_MoveUp.Enabled = true;
                btn_MoveDown.Enabled = true;
            }
        }

        private void CreateActionClick(object sender, EventArgs e)
        {
            CAction action = new CAction() { command = "new_action" };
            actions.Add(action);
            actions[actions.Count - 1].ShowDialog();
            RefreshForm();
        }

        private void OnEdit(object sender, EventArgs e)
        {
            int idx = listBox_actions.SelectedIndex;
            if (idx == -1) return;
            actions[idx].ShowDialog();
            RefreshForm();
        }

        private void DeleteActionClick(object sender, EventArgs e)
        {
            int idx = listBox_actions.SelectedIndex;
            if (idx == -1) return;
            DialogResult result = MessageBox.Show("确定要删除这个动作吗?", "删除动作", MessageBoxButtons.OKCancel);
            if (result == DialogResult.OK)
            {
                actions.RemoveAt(idx);
                RefreshForm();
            }
        }

        private void MoveUpActionClick(object sender, EventArgs e)
        {
            int idx = listBox_actions.SelectedIndex;
            CAction s1 = actions[idx];
            CAction s2 = actions[idx - 1];
            actions[idx] = s2;
            actions[idx - 1] = s1;
            RefreshForm();
        }

        private void MoveDownActionClick(object sender, EventArgs e)
        {
            int idx = listBox_actions.SelectedIndex;
            CAction s1 = actions[idx];
            CAction s2 = actions[idx + 1];
            actions[idx] = s2;
            actions[idx + 1] = s1;
            RefreshForm();
        }

        private void cEvent_FormClosing(object sender, FormClosingEventArgs e)
        {
            e.Cancel = true;
            Visible = false;
        }

        private void cEvent_Load(object sender, EventArgs e)
        {
            RefreshForm();
        }
    }
}
